package com.ym.ganio.gan;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ym.ganio.R;
import com.ym.ganio.define.ReadDefine;
import com.ym.ganio.widget.YmViewPagerTabLayout;

import java.util.ArrayList;
import java.util.Arrays;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link GanMainFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link GanMainFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GanMainFragment extends Fragment {

    private OnFragmentInteractionListener mListener;
    private YmViewPagerTabLayout mViewPagerTabLayout;

    public GanMainFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment GanMainFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static GanMainFragment newInstance(String param1, String param2) {
        GanMainFragment fragment = new GanMainFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_gan_main, container, false);
        setupUI(view);
        return view;
    }

    private void setupUI(View view){
        mViewPagerTabLayout = new YmViewPagerTabLayout();
        final String[] ganTabs = getContext().getResources().getStringArray(R.array.gan_tab);
        mViewPagerTabLayout.setTitleList(Arrays.asList(ganTabs));

        mViewPagerTabLayout.setFragmentPagerAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {

            @Override
            public CharSequence getPageTitle(int position) {
                return ganTabs[position];
            }

            @Override
            public Fragment getItem(int position) {
                GanFragment fragment = new GanFragment();
                Bundle bundle = new Bundle();
                bundle.putString(ReadDefine.PARAM_KEY_TYPE, getPageTitle(position).toString());
                fragment.setArguments(bundle);
                return fragment;
            }

            @Override
            public int getCount() {
                return ganTabs.length;
            }
        });


        ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        tabLayout.setTabMode (TabLayout.MODE_SCROLLABLE);
        mViewPagerTabLayout.setupUI(viewPager, tabLayout, getActivity().getSupportFragmentManager());
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
